#import "SliderViewController.h"
#import "WGRangeSlider.h"

static CGFloat const kViewControllerRangeSliderWidth = 290.0;
static CGFloat const kViewControllerLabelWidth = 100.0;

@interface SliderViewController ()

@property (nonatomic, strong) WGRangeSlider *rangeSlider;
@property (nonatomic, strong) UILabel *label;

@end

@implementation SliderViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Additional setup after loading the view
    self.view.backgroundColor = UIColorFromRGB(0x12134);
    [self setUpViewComponents];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    CGFloat labelX = (CGRectGetWidth(self.view.frame) - kViewControllerLabelWidth) / 2;
    self.label.frame = CGRectMake(labelX, 110.0, kViewControllerLabelWidth, 20.0);

    CGFloat sliderX = (CGRectGetWidth(self.view.frame) - kViewControllerRangeSliderWidth) / 2;
    self.rangeSlider.frame = CGRectMake(sliderX, CGRectGetMaxY(self.label.frame) + 20.0, 290.0, 20.0);
}

#pragma mark - Actions

- (void)rangeSliderValueDidChange:(WGRangeSlider *)slider
{
    [self updateRangeText];
}

#pragma mark - UI

- (void)setUpViewComponents
{
    // Text label
    self.label = [[UILabel alloc] initWithFrame:CGRectZero];
    self.label.backgroundColor = UIColorFromRGB(0x12134);
    self.label.numberOfLines = 1;
    self.label.textColor = UIColorFromRGB(0xcbccd1);

    // Init slider
    self.rangeSlider = [[WGRangeSlider alloc] initWithFrame:CGRectZero];
    self.rangeSlider.backgroundColor = UIColorFromRGB(0x12134);
    [self.rangeSlider addTarget:self
                         action:@selector(rangeSliderValueDidChange:)
               forControlEvents:UIControlEventValueChanged];
    self.rangeSlider.sendInstantUpdates = YES;
    [self.rangeSlider setMinValue:18 maxValue:50];
    [self.rangeSlider setLeftValue:18 rightValue:32];
    self.rangeSlider.minimumDistance = 1;

    
    [self updateRangeText];

    [self.view addSubview:self.label];
    [self.view addSubview:self.rangeSlider];
}

- (void)updateRangeText
{
    NSLog(@"%.f - %.f", self.rangeSlider.leftValue, self.rangeSlider.rightValue);
    self.label.text = [NSString stringWithFormat:@"%.f - %.f+",
                       self.rangeSlider.leftValue, self.rangeSlider.rightValue];
}

@end
